package com.danielmiller.mygradebook;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddGradeItem extends Activity{

	EditText gradeRecv, gradePos, gradeName;
	
	public int class_index, category_index;
	String picName = "", gradeNameString;
	int gradePosInt, gradeRecvInt;
	SQLiteDatabase db;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputiteminfo);
		
		class_index = getIntent().getIntExtra("CLASS_INDEX", -1);
		category_index = getIntent().getIntExtra("CATEGORY_INDEX", -1);
		gradeRecv = (EditText)findViewById(R.id.enter_grade_edit_text);
		gradePos = (EditText)findViewById(R.id.possible_points_edit_text);
		gradeName = (EditText)findViewById(R.id.enter_grade_name_edit_text);
		
		final Button submitButton = (Button) findViewById(R.id.submit_button);
		submitButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				gradeNameString = gradeName.getText().toString();
				gradeRecvInt = Integer.parseInt(gradeRecv.getText().toString());
				gradePosInt = Integer.parseInt(gradePos.getText().toString());
				
				db = openOrCreateDatabase(
						"SEMESTER_DB.db"
						, SQLiteDatabase.CREATE_IF_NECESSARY
						, null
				);
				db.setVersion(1);
				db.setLocale(Locale.getDefault());
				db.setLockingEnabled(true);

				String query = "INSERT INTO tbl_grades" +
					" VALUES (" 
						+ "\"" + class_index + "\"," 
						+ category_index + ","
						+ gradeNameString + "," 
						+ gradeRecvInt + "," 
						+ gradePosInt + "," 
						+ picName + 
					"');";
				db.execSQL(query);
				db.close();

				//Toast
				Toast toast = Toast.makeText(getApplicationContext(), 
						"Grade Info Saved!", Toast.LENGTH_SHORT);
				toast.show();
			}
		});
		
		final Button doneButton = (Button) findViewById(R.id.done_button);
		doneButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(AddGradeItem.this, AddGrade.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", class_index);
				intent.putExtra("CATEGORY_INDEX", category_index);
				startActivity(intent);
			}
		});
		
		final Button picButton = (Button) findViewById(R.id.add_pic_button);
		picButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Open camera, take pic, save pic, store pic location
				
			}
		});
	}
}
